package com.hckjsec.common.utils;

import java.math.BigDecimal;
import java.text.NumberFormat;

public class GrowthRate {
    public String percentBigDecimal(BigDecimal preNum, BigDecimal sufNum){
        double result = countDecimal(preNum,sufNum);
        if(result>0){
            return "+"+result+"%";
        }
        if(result<0){
            return result+"%";
        }
        if(result==0){
            return "+"+0+"%";
        }
        return null;
    }
    public  double countDecimal(BigDecimal preNum,BigDecimal sufNum){
        boolean preBoolean = verifyNum(preNum);
        boolean sufBoolean = verifyNum(sufNum);
        //同时为true计算
        if(preBoolean && sufBoolean){
            boolean b = verifyEqual(preNum, sufNum);
            if (b == false){
                return realCountDecimal(preNum,sufNum);
            }
            if (b){
                return 0;
            }
        }
        if(preBoolean == false && sufBoolean ==false){
            return 0;
        }
        if(sufBoolean ==false){
            return 100;
        }
        return  0;
    }
    //验证数字是否为零和null
    public boolean verifyNum(BigDecimal num){
        if(null !=num && num.compareTo(BigDecimal.ZERO)!=0 ){
            return true;
        }
        return false;
    }

    //验证两个数字是否相等
    public boolean verifyEqual(BigDecimal preNum,BigDecimal sufNum){
        int n = preNum.compareTo(sufNum);
        //比较 -1 小于   0 等于    1 大于
        if(n==0){
            return true;
        }
        return false;
    }
    //真正计算
    public double realCountDecimal(BigDecimal preNum,BigDecimal sufNum){
        //(前面的数字-后面的数字)/后面的数字*100
        BigDecimal bigDecimal = (preNum.subtract(sufNum)).divide(sufNum).multiply(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_UP);
        if (bigDecimal.compareTo(BigDecimal.ZERO) !=0){
            return  bigDecimal.doubleValue();
        }
        return 0;
    }


    /**
     *  **计算月增长率**
     *    sameMonth 本月金额
     *    lastMonth 上月金额
     */
    public static String getAnalysisData(int sameMonth, int lastMonth) {
        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(0);
        Integer growthNum=0;
        String growthPercent="0";
        if (lastMonth > 0 && sameMonth > 0) {
            if (lastMonth < sameMonth) {
                //如果下个数大于上个数，则增长率 为 正
                growthNum=sameMonth - lastMonth;
                growthPercent=  numberFormat.format((float)growthNum / (float) lastMonth * 100)+ "%";
                return  "+"+growthPercent;
            } else if (lastMonth > sameMonth) {
                //如果下个数小于上个数，则增长率 为 负
                growthNum=lastMonth - sameMonth;
                growthPercent=  numberFormat.format((float)growthNum / (float) sameMonth * 100)+ "%";
                return  "-"+growthPercent;

            } else {
                //如果相等，增长率为 0
                return "0%";
            }
        } else if (lastMonth > 0 && sameMonth == 0) {
            //如果上个数大于0，下个数为0，增长率为 0
            return "0%";
        } else if (lastMonth == 0 && sameMonth > 0) {
            //如果下个数大于0，上个数为0，增长率为 0
            growthNum= sameMonth- lastMonth;
            growthPercent=  numberFormat.format((float)growthNum / (float) sameMonth * 100)+ "%";
            return  growthPercent;

        } else
        {
            //如果相等，增长率为 0
            return "0%";
        }
    }


    public static void main(String[] args) {
        String percent = getAnalysisData(0, 6);
        System.out.println(percent);
    }
}
